package com.yun.csv2db.reader;

import com.yun.csv2db.entity.Student;
import com.yun.csv2db.entity.enums.SexEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;


public class StudentFieldSetMapper implements FieldSetMapper<Student> {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    public Student mapFieldSet(FieldSet fieldSet) throws BindException {
        Student student = new Student();

        student.setId(fieldSet.readInt("id"));
        student.setName(fieldSet.readString("name"));
        student.setSex(SexEnum.getEnumFromString(fieldSet.readString("sex")));
        student.setBirthDate(fieldSet.readString("birthDate"));
        student.setHomeAddress(fieldSet.readString("homeAddress"));
        student.setSchool(fieldSet.readString("school"));
        student.setGrade(fieldSet.readString("grade"));

        student.setUpdateTime();
        logger.info("New student:" + student);
        return student;
    }
}
